/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import * as path from 'path';
import { ProjectStructureOpt } from '../../../../src/base/common/options/project-structure-opt';
import { Json5Reader } from '../../../../src/base/util/json5-reader';
import { ValidateUtil } from '../../../../src/base/util/validate-util';
import { HvigorLogger } from '../../../../src/base/log/hvigor-log';

jest.spyOn(HvigorLogger.prototype, 'errorMessageExit').mockImplementation(
  (message: string, ...args: unknown[]) => {
    throw new Error((message));
  });

describe('test validate', () => {

  test('test validate module name', () => {
    const json5FilePath = path.resolve('test/resource/common/error/lack-module-name.json5');
    const projectStructureOpt: ProjectStructureOpt = Json5Reader.getJson5Obj(json5FilePath);

    expect(() => {
      projectStructureOpt.modules.forEach((module) => {
        ValidateUtil.validateModule(module);
      });
    }).toThrow(new Error('The required attribute: module-name is missing ' +
      'in the project-level build-profile.json5 file.'));
  });

  test('test validate module srcPath', () => {
    const json5FilePath1 = path.resolve('test/resource/common/error/lack-module-srcPath.json5');
    const projectStructureOpt1: ProjectStructureOpt = Json5Reader.getJson5Obj(json5FilePath1);

    expect(() => {
      projectStructureOpt1.modules.forEach((module) => {
        ValidateUtil.validateModule(module);
      });
    }).toThrow(new Error('The required attribute module-srcPath is missing ' +
      'in the project-level build-profile.json5 file.'));
  });

});
